package com.snsoft.sntvreplay;


import java.net.URLDecoder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Browser;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.net.NetworkInfo;

public class YoutubePlayerActivity extends Activity 
{
	private WebView wvplayer;
	private Dialog dgplayer;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState); 
    	final Activity activity = this; 
    	
        setContentView(R.layout.player);
        
        wvplayer = (WebView)findViewById(R.id.wvplayer);
        
        wvplayer.getSettings().setJavaScriptEnabled(true);
        wvplayer.getSettings().setLoadsImagesAutomatically(true);
        wvplayer.getSettings().setLightTouchEnabled(true);
        wvplayer.getSettings().setPluginsEnabled(true);
        wvplayer.setVerticalScrollBarEnabled(false);
        wvplayer.setHorizontalScrollBarEnabled(false);
        wvplayer.setVerticalScrollbarOverlay(false);

        wvplayer.setBackgroundColor(Color.BLACK);
        
        // Loading dialog set
        dgplayer = new Dialog(this, R.style.SNDialog);
        dgplayer.addContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        wvplayer.setWebViewClient(new WebViewClient()
        {
        	// Webview handler
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url)
        	{
        		// 한글인코딩
        		try{ url = URLDecoder.decode(url, "UTF-8"); }catch(Exception e){} 
        		
        		view.loadUrl(url);

        		return true;
        	}
        	
        	
        	@Override     
        	public void onPageStarted(WebView view, String url, Bitmap favicon) 
        	{
        		
        		try{ dgplayer.show(); }catch(Exception e){} 
        	} 
        	
        	@Override     
        	public void onPageFinished(WebView view, String url) 
        	{
        		
        		if(dgplayer.isShowing())
        		{
        			try{ dgplayer.dismiss(); }catch(Exception e){} 
        		}
        	}      
        	/*
        	@Override
        	public void onReceivedError(WebView view, int errorCode,String description, String failingUrl)
        	{
        		//wv.loadData("<html><body align='center' valign='center'></body></html>", "text/html", "UTF-8");
        		Toast.makeText(activity, "Network unavailable. Please try again later.", Toast.LENGTH_LONG).show();
        	}
        	*/
        });
        
        //wvplayer.loadUrl("http://www.youtube.com/embed/?listType=playlist&list=PL04520E246B86D404&autoplay=1");
        Bundle extras = getIntent().getExtras();
        String pl_id = extras.getString("PL_ID");
        
        String player_html = "<!DOCTYPE >";
        player_html += "<html lang='ko'>";
        player_html += "<head>";
        player_html += "<meta charset='UTF-8' />";
        player_html += "<title>SNTV-무료TV다시보기</title>";
        player_html += "<meta name='author' content='TACO' />";
        player_html += "<meta name='format-detection' content='telephone=no' />";
        player_html += "<meta name='keywords' content='tv,kbs,mbc,replay' />";
        player_html += "<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=medium-dpi' />";
        player_html += "<link rel='apple-touch-icon' href='icon@2x.png'/>";
        player_html += "<link rel='stylesheet' href='file:///android_asset/css/mobile.css' />";
        player_html += "</head>";
        player_html += "<body id='player_body'>";
        player_html += "<object width='100%' height='100%'>";
        player_html += "<param name='movie' value='https://www.youtube.com/v/videoseries?&listType=playlist&list=" + pl_id + "&version=3'></param>";
        player_html += "<param name='allowFullScreen' value='true'></param>";
        player_html += "<param name='allowScriptAccess' value='always'></param>";
        player_html += "<embed src='https://www.youtube.com/v/videoseries?listType=playlist&list=" + pl_id + "&version=3' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='100%' height='100%'></embed>";
        player_html += "</object>";
        player_html += "</body>";
        player_html += "</html>";
        
        player_html = player_html.replace("%", "%25");
        player_html = player_html.replace("#", "%23");
        player_html = player_html.replace("\\", "%27");
        player_html = player_html.replace("?", "%3f");
        
        //wvplayer.loadData(player_html, "text/html", "UTF-8");
        //wvplayer.loadUrl("file:///android_asset/player.html");
        wvplayer.loadUrl("http://www.youtube.com/embed/?listType=playlist&list=" + pl_id + "&autoplay=1");

    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	if(keyCode == KeyEvent.KEYCODE_BACK)
    	{
    		wvplayer.loadData("", "text/html", "UTF-8");
    		YoutubePlayerActivity.this.finish();
    	}
    	
    	return super.onKeyDown(keyCode, event);
    }
    
    @Override
    protected void onStart() 
    {
    	// 화면 절전 모드 해제
    	super.onStart();
    	getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    
    @Override
    protected void onStop() 
    {
    	// 화면 절전 모드 복구
    	super.onStop();
    	getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}
